<?php
class ProfileAction extends Action{
	private $user;

	public function __construct()
	{
		parent::__construct();

		$this->user = new UserModel();

		if($this->session->getAttr("uid") == null){
			$this->forward("Index");
		}
	}
	
	protected function init()
	{

	}
	public function display()
	{
		
	}
	public function doView()
	{
		$ws = new WorksetModel();
		$w = new WorkModel();

		$uid = $this->get['uid'] ? $this->get['uid'] : $this->session->getAttr('uid');
		$uid = (int)$uid;
		$user = $this->get['uid'] ? null : $this->session->getAttr('user');

		if(!$uid)
		{
			$this->showMessage("用户不存在!");
		}

		if($user == null)
		{
			$u = new UserModel();
			$u->addCondition('uid', '=', $uid, '');
			$user = $u->findOne();
			if(!$user)
			{
				$this->showMessage("用户不存在!");
			}
		}

		$ws->addCondition('author_id' , '=', $uid, "");
		$w->addCondition('author_id' , '=', $uid, "");

		$ws_count = $ws->count("works_set_id");
		$work_count = $w->count("work_id");
		$w->addCondition('author_id' , '=', $uid, "");

		$this->smarty->assign('ws_count', $ws_count);
		$this->smarty->assign('work_count', $work_count);

		$this->smarty->assign('user', $user);

		$this->smarty->display("profile.tpl");
	}
	public function profile()
	{
		global $degrees, $user_types;

		$sch = new SchoolModel();
		$schools = $sch->findAll();

		$options = array();

		$options = fetchOptions($schools, 'sid', 'school_name');

		$this->user->addCondition('uid', '=', $this->session->getAttr('uid'), '');
		$user = $this->user->findOne();
		$this->session->setAttr('user', $user);

		$birth = $user['birth_year'] . '-'. $user['birth_month'] . '-' . $user['birth_day'];
		$work_time = $user['career_start'];

		$this->smarty->assign('birth', $birth);
		$this->smarty->assign('work_time', $work_time);
		$this->smarty->assign('schools', $options);
		$this->smarty->assign('degrees', $degrees);
		$this->smarty->assign('user_types', $user_types);
		$this->smarty->assign('user', $user);
		$this->smarty->display("Profile-profile.tpl");
	}

	public function doUpdateBasic()
	{
		$signature = array(
			 'email' => array('required' => true, 'type' => 'string'),
			 'truename' => array('required' => true, 'type' => 'string'),
			 'sex' => array('required' => true, 'type' => 'int'),
			 'BirthYear' => array('required' => true, 'type' => 'int'),
			 'BirthMonth' => array('required' => true, 'type' => 'int'),
			 'BirthDay' => array('required' => true, 'type' => 'int'),
			 'home_province' => array('required' => true, 'type' => 'string'),
			 'home_city' => array('required' => true, 'type' => 'string')
			);

		$this->checkVars($this->post, $signature);
		$this->post['birth_year'] = $this->post['BirthYear'];
		$this->post['birth_month'] = $this->post['BirthMonth'];
		$this->post['birth_day'] = $this->post['BirthDay'];

		$this->doUpdate();
	}
	public function doUpdateContact()
	{
		$sigs = array(
			 'qq_number' => array('required' => false, 'type' => 'string'),
			 'msn_account' => array('required' => false, 'type' => 'string'),
			 'cellphone' => array('required' => false, 'type' => 'string'),
			 'telephone' => array('required' => false, 'type' => 'string'),
			 'homepage' => array('required' => false, 'type' => 'string')
			);
		$this->checkVars($this->post, $sigs);
		$this->doUpdate();
	}
	public function doUpdateSchool()
	{
		$sigs = array(
			 'school_id' => array('required' => true, 'type' => 'int'),
			 'department_id' => array('required' => false, 'type' => 'int'),
			 'user_type' => array('required' => true, 'type' => 'int'),
			 'degree' => array('required' => true, 'type' => 'int')
			);
		$this->checkVars($this->post, $sigs);
		$this->doUpdate();
	}
	public function doUpdateWork()
	{
		$sigs = array(
			 'company_name' => array('required' => false, 'type' => 'string'),
			 'company_description' => array('required' => false, 'type' => 'string'),
			 'business' => array('required' => false, 'type' => 'string'),
			 'position' => array('required' => false, 'type' => 'string'),
			 'career_start' => array('required' => false, 'type' => 'string'),
			 'WorkYear' => array('required' => false, 'type' => 'int'),
			 'WorkMonth' => array('required' => false, 'type' => 'int'),
			 'WorkDay' => array('required' => false, 'type' => 'int')
			);
		$this->checkVars($this->post, $sigs);
		$this->post['career_start'] = $this->post['WorkYear'] . '-' . $this->post['WorkMonth'] . '-' . $this->post['WorkDay'];
		$this->doUpdate();
	}
	public function doUpdateOther()
	{
		$sigs = array(
			 'privacy' => array('required' => true, 'type' => 'int')
			);
		// TODO: 处理照片上传
		$this->checkVars($this->post, $sigs);
		$this->doUpdate();
	}
	public function doUpdatePw()
	{
		$sigs = array(
			 'password' => array('required' => true, 'type' => 'string'),
			 'new_password' => array('required' => true, 'type' => 'string'),
			 'new_password2' => array('required' => true, 'type' => 'string')
			);
		// TODO: 验证旧密码，检查两次输入的一致性
		$this->checkVars($this->post, $sigs);
		$this->doUpdate();
	}
	public function doUpdate()
	{
		$this->user->create();

		$this->user->uid = $this->session->getAttr('uid');
		$this->user->addCondition('uid' , '=', $this->user->uid, '');

		//echo $this->user->toSql("update");
		if($this->user->update())
		{
			$msg = "更改信息成功！";
		}else{
			$msg = "更改信息失败！";
		}
		$this->showMessage($msg);
	}
};
?>
